package com.mojo.kafka.controller;

import com.mojo.kafka.bean.User;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * rocket 生产者
 *
 * @author <a href="mailto:mojo_jj@163.com">Mojo</a>
 */
@RestController
@RequestMapping("/rocket")
public class RocketProducerController {
    private static final String TOPIC = "rocket-topic";

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/send")
    public void send() {
        User user = new User();
        user.setName("rocker");
        user.setAge(18);
        DefaultMQProducer producer = new DefaultMQProducer();
        Message<User> message = MessageBuilder.withPayload(user).build();
        rocketMQTemplate.send(TOPIC, message);
    }
}
